package com.example.constraintlayoutstudy;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.constraintlayoutstudy.check.Check;
import com.example.constraintlayoutstudy.check.LoginStatusCheck;
import com.example.constraintlayoutstudy.popupwindow.HorizontalPosition;
import com.example.constraintlayoutstudy.popupwindow.SmartPopupWindow;
import com.example.constraintlayoutstudy.popupwindow.TestPopupWindow;
import com.example.constraintlayoutstudy.popupwindow.VerticalPosition;

import static com.example.constraintlayoutstudy.popupwindow.SmartPopupWindow.makeDropDownMeasureSpec;

public class MainActivity extends AppCompatActivity {
  private TestPopupWindow mWindow;
  private View mPopupContentView;
  private TextView textView;
  private Button btn_login,button7;

  private String text1="请在壁挂炉仅水泵工作时，观察APP对应控制页面，即当前上一级温控页面所显示功率值，误差在10W以内，尽量填写整10数值。";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWindow = new TestPopupWindow(this);
    mPopupContentView = getLayoutInflater().inflate(R.layout.layout_flaot, null);

    //View contentView = mWindow.getContentView();
    //需要先测量，PopupWindow还未弹出时，宽高为0
    mPopupContentView.measure(makeDropDownMeasureSpec(mWindow.getWidth()),
        makeDropDownMeasureSpec(mWindow.getHeight()));

    textView = findViewById(R.id.textView4);
    textView.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {

      }
    });

    btn_login = findViewById(R.id.button_login);
    btn_login.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        //toLoginActivity();
        showWindow(text1);
      }
    });
    button7 = findViewById(R.id.button7);
    button7.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        //toLoginActivity();
        showWindow(text1);
      }
    });
  }

  @Check(LoginStatusCheck.class)
  private void toLoginActivity() {
    startActivity(new Intent(MainActivity.this, LoginActivity.class));
  }


  private void showWindow(String text) {

    int mGravity = Gravity.TOP;
    int mOffsetX = -mWindow.getContentView().getMeasuredWidth();
    int mOffsetY = 50;
    // 常规方式显示window
    //PopupWindowCompat.showAsDropDown(mWindow,btn_login,mOffsetX,mOffsetY,mGravity);
    //使用SmartPopup
    SmartPopupWindow smartPopupWindow = SmartPopupWindow.Builder
        .build(MainActivity.this, mPopupContentView)
        .createPopupWindow();
    smartPopupWindow.showAtAnchorView(button7, VerticalPosition.ABOVE, HorizontalPosition.CENTER);
  }

  private void showWindowLeft(String text) {
    TextView tv_flaot_text = mPopupContentView.findViewById(R.id.tv_flaot_text);
    tv_flaot_text.setText(text);
    int mGravity = Gravity.START;
    int mOffsetX = -mWindow.getContentView().getMeasuredWidth();
    int mOffsetY = 0;
    //使用SmartPopup
    SmartPopupWindow.Builder
        .build(MainActivity.this, mPopupContentView)
        .createPopupWindow()
        .showAtAnchorView(btn_login, VerticalPosition.BELOW, HorizontalPosition.LEFT);
  }

  private void showWindowRight(String text) {
    TextView tv_flaot_text = mPopupContentView.findViewById(R.id.tv_flaot_text);
    tv_flaot_text.setText(text);
    int mGravity = Gravity.START;
    int mOffsetX = -mWindow.getContentView().getMeasuredWidth();
    int mOffsetY = 0;
    //使用SmartPopup
    SmartPopupWindow.Builder
        .build(MainActivity.this, mPopupContentView)
        .createPopupWindow()
        .showAtAnchorView(btn_login, VerticalPosition.BELOW, HorizontalPosition.RIGHT);
  }
}
